import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String result = "未知错误";
        // 1.得到参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        // 2.业务处理
        // 非空效验
        if (null != username && null != password &&
                !username.equals("") && !password.equals("")) {
            if (username.equals("admin") && password.equals("admin")) {
                result = "恭喜：登录成功！";
            } else {
                result = "登录失败，用户名或密码输入错误，请检查！";
            }
        } else {
            result = "非法参数！";
        }

        resp.setContentType("text/html; charset=utf-8");

        // 3.将结果返回给前端
        resp.getWriter().write(result);
    }
}
